-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
53 lines (43 loc) · 1.55 KB
/
Solution.java
File metadata and controls
53 lines (43 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class TopologicalSort {
static void topologicalSortUtil(int v, boolean[] visited, Stack<Integer> stack, List<List<Integer>> graph) {
visited[v] = true;
for (int neighbor : graph.get(v)) {
if (!visited[neighbor]) {
topologicalSortUtil(neighbor, visited, stack, graph);
}
}
stack.push(v);
}
static void topologicalSort(List<List<Integer>> graph, int vertices) {
boolean[] visited = new boolean[vertices];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
topologicalSortUtil(i, visited, stack, graph);
}
}
System.out.print("Topological Sort: ");
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of vertices and edges: ");
int vertices = scanner.nextInt();
int edges = scanner.nextInt();
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < vertices; i++) {
graph.add(new ArrayList<>());
}
System.out.println("Enter the edges (u -> v):");
for (int i = 0; i < edges; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
graph.get(u).add(v);
}
topologicalSort(graph, vertices);
}
}